<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="common/header.jsp" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>新增一个项目</title>
</head>
<%-- <script type="text/javascript" src="${webRoot}/layer/layui.js"></script> --%>
<style>

.layui-input-block div{
	margin-bottom:1rem;
}
h2{
	margin-top:0px;
	margin-bottom:0px;
}

</style>

<body>

<form class="layui-form" action="${webRoot}/svn/e" style="padding:1rem;">
	<input name="id" type="hidden" value="${pro.id}" />
	<div class="layui-form-item">
    <div class="layui-input-block">
    	<div>项目: ${pro.name}</div>
    	<div>svn目录地址: ${pro.svnDir}</div>
    	<div>本地部署路径: ${pro.localDir}</div>
    	<div>补丁存放路径: ${pro.patchDir}</div>
    	<div>打包区间: <span id="versionStart">_ _</span>&nbsp;至&nbsp;<span>${svnLogs.get(0).version}</span>
    		<button class="layui-btn layui-btn-radius layui-btn-primary layui-btn-small" style="float:right;" onclick="getPatchDetail();return false;">生成打包目录</button>
    	</div>
    </div>
  </div>
	<div class="layui-collapse" lay-accordion>
		<div class="layui-colla-item">
			<h2 class="layui-colla-title">提交日志</h2>
			<div class="layui-colla-content layui-show" style="padding:0px;">
			<table class="layui-table" style="margin:0px;">
				<colgroup>
					<col width="150">
					<col width="200">
					<col>
				</colgroup>
				<thead>
					<tr>
						<th>版本号</th>
						<th>提交人</th>
						<th>提交时间</th>
						<th>选择打包开始时间</th>
					</tr> 
				</thead>
				<tbody id="logContent">
				<c:forEach items="${svnLogs}" var="item" varStatus="statu">
					<tr>
						<td>${item.version}</td>
						<td>${item.commitUser}</td>
						<td><fmt:formatDate value="${item.commitTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
						<td>
							<c:if test="${statu.index!=0}">
								<span class="layui-badge layui-bg-orange" style="cursor:pointer;" onclick="addToStart('${item.version}', this)">+</span>
							</c:if>
						</td>
					</tr>
				</c:forEach>
				</tbody>
			</table>
			</div>
		</div>
		<div class="layui-colla-item">
			<h2 class="layui-colla-title">打包详情</h2>
			<div class="layui-colla-content"  style="padding:0px;">
			<table class="layui-table" style="margin:0px;">
				<colgroup>
					<col width="150">
					<col width="200">
					<col>
				</colgroup>
				<thead>
					<tr>
<!-- 						<th>补丁文件<button class="layui-btn layui-btn-radius layui-btn-primary layui-btn-small" style="float:right;" onclick="doPatch();return false;">打包</button></th> -->
						<th>补丁文件</th>
					</tr> 
				</thead>
				<tbody id="patchFile"></tbody>
				<thead>
					<tr>
						<th>忽略文件</th>
					</tr>
				</thead>
				<tbody id="ignoreFile"></tbody>
				<thead>
					<tr>
						<th>需删除文件</th>
					</tr> 
				</thead>
				<tbody id="removeFile"></tbody>
			</table>
			</div>
		</div>
	</div>
</form>

</body>
</html>
<script>

layui.use('element', function(){
	var element = layui.element;
});

var ignorestr = '${pro.ignores}';
var ignores;
if(ignorestr != null && ignorestr!=''){
	ignores = ignorestr.split(',');
	var html = '';
	$.each(ignores, function(i, item){
		html += '<tr>';
		html += '<td><input type="hidden" value="'+item+'"/>'+item+'<span class="layui-badge layui-bg-orange" style="float:right;cursor:pointer;" onclick="toIgnore(\''+item+'\',this)">&nbsp;-&nbsp;</span></td>';
		html += '</tr>';
	});
	$('#ignoreFile').html(html);
}else{
	ignores = new Array();
}

function addToStart(version, s){
	$('#logContent>tr>td>span').attr('class', 'layui-badge layui-bg-orange');
	$(s).removeClass('layui-bg-orange').addClass('layui-bg-gray');
	$('#versionStart').text(version);
}

var copyList = '';
function getPatchDetail(){
	var versionStart =  $('#versionStart').text();
	if(versionStart == '_ _'){
		layer.msg('请选择打包开始时间');
		return;
	}
	
	$.ajax({
		url:'${webRoot}/patch/diff',
		type:'post',
		data:{'versionStart':versionStart, 'versionEnd':'${svnLogs.get(0).version}', 'projectId':'${pro.id}'},
		success:function(data){
			var html = '';
			copyList = data.copyList;
			$.each(data.copyList, function(i, item){
				if($.inArray(item, ignores) == -1){
					html += '<tr>';
					html += '<td><input type="hidden" value="'+item+'"/>'+item+'<span class="layui-badge layui-bg-orange" style="float:right;cursor:pointer;" onclick="toIgnore(\''+item+'\',this)">&nbsp;-&nbsp;</span></td>';
					html += '</tr>';
				}
			});
			$('#patchFile').html(html);
			html = '<tr><td>';
			$.each(data.removeList, function(i, item){
				html += '<input type="hidden" value="'+item+'"/>'+item+'<br/>';
			});
			html += '</td></tr>';
			$('#removeFile').html(html);
			$('h2:eq(1)').trigger('click');
		}
	});
}

function toIgnore(ignoreFile, s){
	var html = $(s).parent().parent().html();
	html = '<tr>'+html.replace('toIgnore', 'toPath')+'</tr>';
	$('#ignoreFile').append(html);
	$(s).parent().parent().remove();
}

function toPath(ignoreFile, s){
	if(copyList == ''){
		layer.msg('未生成打包目录');
		return;
	}
	var html = $(s).parent().parent().html();
	html = '<tr>'+html.replace('toPath', 'toIgnore')+'</tr>';
	$(s).parent().parent().remove();
	$.each(copyList, function(i, item){
		if(ignoreFile == item){
			$('#patchFile').append(html);
		}
	});
}

function doPatch(){
	if(copyList == ''){
		layer.msg('未生成打包目录');
		return;
	}
	
	var patchFile = getVal('#patchFile input');
	var ignoreFile = getVal('#ignoreFile input');
	var removeFile = getVal('#removeFile input');
	var versionStart = $('#versionStart').text();
	var versionEnd = $('#versionStart').next('span').text();
	$.ajax({
		url:'${webRoot}/patch/dop',
		type:'post',
		data:{'patchFile':patchFile, 'ignoreFile':ignoreFile, 'removeFile':removeFile, 'projectId':'${pro.id}','versionStart':versionStart,'versionEnd':versionEnd},
		success:function(data){
			if(data.success){
				//layer.msg('补丁生成成功',function(){window.parent.winClose(false);});
				alert('补丁生成成功');
				window.parent.winClose(false);
			}else{
				alert(data.msg);
			}
		}
	});
}

function getVal(selector){
	var a = $(selector);
	if(a==undefined || a.length==0){
		return '';
	}
	var as = '';
	a.each(function(i,item){
		as += a.eq(i).val()+',';
	});
	return as.substring(0, as.length-1);
}

</script>